var oForm = document.querySelector("form");
var oTel = document.querySelector("#tel");
var oPass = document.querySelector("#pass");
var rule = [
    {
        reg: /^1[3-9]\d{9}$/,
        err: '请输入正确的账号！',
    },
    {
        reg: /^[a-z0-9]{6,12}$/,
        err: '密码错误！',
    }
]
oForm.addEventListener("submit", loginFn);
function loginFn() {
    var tel = oTel.value;
    var pass = oPass.value;
    var user = JSON.parse(localStorage.getItem("user"));
    var index = test(user, tel, pass);
    if (rule[0].reg.test(tel) == true) {
        if (rule[1].reg.test(pass) == true) {
            if (index != -1) {
                alert("登录成功！");
                localStorage.setItem("userTel", user[index].tel);
            } else {
                alert("登录失败！");
                event.preventDefault();
            }
        } else {
            alert(rule[1].err);
            event.preventDefault();
        }
    } else {
        alert(rule[0].err);
        event.preventDefault();
    }
}
function test(user, userTel, password) {
    for (var i = 0; i < user.length; i++) {
        if (user[i].tel == userTel && user[i].pass == password) {
            return i;
        }
    }
    return -1;
}